package com.example.comsys.control;

import com.example.comsys.dao.DepartmentDAO;
import com.example.comsys.dataobject.DepartmentDO;
import com.example.comsys.model.Department;
import com.example.comsys.model.Result;
import com.example.comsys.service.DepartmentService;
import com.example.comsys.util.ComSysConstant;
import com.example.comsys.util.ConvertUtils.DepartmentUtils;
import com.example.comsys.util.ResultUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * depart contraoller
 *
 * @author wjh
 * @date 2020/1/4, 周六
 */
@RestController
@RequestMapping(ComSysConstant.API_PREFIX + "/department")
public class DepartmentController {

  @Autowired
  private DepartmentDAO departmentDAO;

  @Autowired
  private DepartmentService departmentService;

  @GetMapping("/list.json")
  public Result<List<Department>> queryAllDepartments() {

    List<DepartmentDO> departmentDos = departmentDAO.selectAll();
    if (CollectionUtils.isEmpty(departmentDos)) {
      return ResultUtils.newSuccessfulResult(new ArrayList<>());
    } else {
      List<Department> departments = departmentDos.stream().map(DepartmentUtils::departmentToModel)
          .filter(Objects::nonNull).collect(Collectors.toList());

      return ResultUtils.newSuccessfulResult(departments);
    }
  }
}
